package controllers

import (
	"shudao-chat-go/models"
	"strings"

	"github.com/beego/beego/v2/server/web"
)

type SceneController struct {
	web.Controller
}

// 隐患识别获取历史记录
func (c *SceneController) GetHistoryRecognitionRecord() {
	user_id, _ := c.GetInt64("user_id")
	if user_id == 0 {
		user_id = 1
	}
	var recognitionRecords []models.RecognitionRecord
	models.DB.Model(&models.RecognitionRecord{}).Where("user_id = ? AND is_deleted = ?", user_id, 0).Order("-updated_at").Find(&recognitionRecords)

	//计算返回的总共的数据数量
	var total int64
	models.DB.Model(&models.RecognitionRecord{}).Where("user_id = ? AND is_deleted = ?", user_id, 0).Count(&total)
	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
		"data":       recognitionRecords, // 直接返回原始数据，不处理关联查询
		"total":      total,
	}
	c.ServeJSON()
}

// 获取识别记录详情
func (c *SceneController) GetRecognitionRecordDetail() {
	recognition_record_id, _ := c.GetInt64("recognition_record_id")
	if recognition_record_id == 0 {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 400,
			"msg":        "识别记录ID不能为空",
		}
		c.ServeJSON()
		return
	}

	var record models.RecognitionRecord
	if err := models.DB.Where("id = ? AND is_deleted = ?", recognition_record_id, 0).First(&record).Error; err != nil {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 404,
			"msg":        "识别记录不存在",
		}
		c.ServeJSON()
		return
	}

	// 将Description字符串转换为数组
	var thirdScenes []string
	if record.Description != "" {
		thirdScenes = strings.Split(record.Description, " ")
	}

	// 通过关联表查询二级场景名称
	// var secondSceneNames []string
	// var recognitionRecordSecondScenes []models.RecognitionRecordSecondScene
	// models.DB.Preload("SecondScene").Where("recognition_record_id = ?", record.ID).Find(&recognitionRecordSecondScenes)

	// for _, rrss := range recognitionRecordSecondScenes {
	// 	secondSceneNames = append(secondSceneNames, rrss.SecondScene.SecondSceneName)
	// }

	// 构建详情数据
	detailData := map[string]interface{}{
		"id":                    record.ID,
		"original_image_url":    record.OriginalImageUrl,
		"recognition_image_url": record.RecognitionImageUrl,
		"user_id":               record.UserID,
		"title":                 record.Title,
		"description":           record.Description,
		"created_at":            record.CreatedAt,
		"updated_at":            record.UpdatedAt,
		// 添加与隐患识别接口一致的字段
		"labels":       record.Labels, // 二级场景名称数组
		"third_scenes": thirdScenes,   // 三级场景名称数组
		"tag_type":     record.TagType,
	}

	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
		"data":       detailData,
	}
	c.ServeJSON()
}

// 获取隐患识别三级场景标题查询正确和错误的示例图
func (c *SceneController) GetThirdSceneExampleImage() {
	third_scene_name := c.GetString("third_scene_name")
	if third_scene_name == "" {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 400,
			"msg":        "三级场景名称不能为空",
		}
		c.ServeJSON()
		return
	}
	var thirdScene models.ThirdScene
	models.DB.Where("third_scene_name = ? and is_deleted = ?", third_scene_name, 0).First(&thirdScene)
	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
		"data":       thirdScene,
	}
	c.ServeJSON()
}
